package book.ch9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class PoliceListener implements ActionListener{
    WindowsView view;
    public PoliceListener(WindowsView view){
        this.view=view;
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==view.inputText){
            String str=view.inputText.getText();
            view.textShow.append("\""+str+"\"的长度："+str.length()+"\n");
        }
        else if(e.getSource()==view.button){
            String str=view.button.getText();
            view.textShow.append(str+"\n");
        }
    }
}

class WindowsView extends JFrame{
    public JTextField inputText;
    public JTextArea textShow;
    public JButton button;
    public PoliceListener listener;

    public WindowsView(){
        init();

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init(){
        setBounds(100,100,600,460);
        setLayout(new FlowLayout());
        Font font=new Font("宋体",Font.PLAIN,20);
        inputText=new JTextField(20);
        inputText.setFont(font);
        button=new JButton("确定");
        button.setFont(font);
        font=new Font("宋体",Font.PLAIN,22);
        textShow=new JTextArea(9,30);
        textShow.setFont(font);
        listener=new PoliceListener(this);

        inputText.addActionListener(listener);
        button.addActionListener(listener);


        add(inputText);
        add(button);
        add(new JScrollPane(textShow));
    }
}

public class E9_7 {
    public static void main(String[] args) {
        WindowsView win=new WindowsView();
    }
}
